//
//  HPReleaseCommentScrollView.m
//  fruitsDealers
//
//  Created by sgone on 16/3/25.
//  Copyright © 2016年 com.sgone. All rights reserved.
//

#import "HPReleaseCommentScrollView.h"
#import "UIView+Helper.h"
#import "Common.h"
#import "Toast+UIView.h"
@interface HPReleaseCommentScrollView () <UITextViewDelegate> {
    
    
}

@property (nonatomic, strong)UILabel *addLabel;
@property (nonatomic, strong)UILabel *placeholderLabel;

//显示字数的lable
@property (nonatomic, weak) UILabel *numLable;

@end

@implementation HPReleaseCommentScrollView

+ (instancetype)initWithFrame:(CGRect)frame {
    
    return [[self alloc] initWithFrame:frame];
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        _contentTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height / 4)];
        _contentTextView.textColor = [UIColor lightGrayColor];
        _contentTextView.font = [UIFont systemFontOfSize:14 / FDLaOt];
        _contentTextView.hidden = NO;
        _contentTextView.delegate = self;
        _contentTextView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_contentTextView];
        
//        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:) name:@"UITextViewTextDidChangeNotification" object:nil];
        
        _placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(4, 4, ViewWidth, 20 / FDLaOt)];
        _placeholderLabel.text = @"说点什么吧~ ~";
        _placeholderLabel.enabled = NO;
        _placeholderLabel.textColor = FDGFont_COLOR;
        _placeholderLabel.font = [UIFont systemFontOfSize:14 / FDLaOt];
        _placeholderLabel.backgroundColor = [UIColor clearColor];
        [_contentTextView addSubview:_placeholderLabel];
        
        UILabel *numLable = [[UILabel alloc] initWithFrame:CGRectMake(ViewWidth - 100, self.contentTextView.height - 25, 100, 20)];
        numLable.font = [UIFont systemFontOfSize:12 / FDLaOt];
        numLable.textAlignment = NSTextAlignmentRight;
        [self addSubview:numLable];
        self.numLable = numLable;
        
        
    
        self.addImageButton = [[UIButton alloc] initWithFrame:CGRectMake(8, self.contentTextView.height, 54 / FDLaOt, 54 / FDLaOt)];
        [self.addImageButton setImage:[UIImage imageNamed:@"compose_pic_add"] forState:UIControlStateNormal];
        [self.addImageButton setImage:[UIImage imageNamed:@"compose_pic_add_highlighted"] forState:UIControlStateHighlighted];
        [self.addImageButton addTarget:self action:@selector(addPhotoClick) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:self.addImageButton];
        
        self.addImageLable = [[UILabel alloc] initWithFrame:CGRectMake(self.addImageButton.x * 2 + self.addImageButton.width, self.addImageButton.y, 70 / FDLaOt, self.addImageButton.height)];
        self.addImageLable.font = [UIFont systemFontOfSize:14 / FDLaOt];
        self.addImageLable.textColor = FDMFont_COLOR;
        self.addImageLable.text = @"添加照片";
        [self addSubview:self.addImageLable];
    }
    return self;
}

- (void)textViewDidChange:(UITextView *)textView {
    
    if (textView.text.length == 0) {
        
        _placeholderLabel.text = @"说点什么吧~ ~";
    } else {
        
        _placeholderLabel.text = @"";
    }
    
    NSInteger number = [textView.text length];
    if (number > self.maxLength) {
//        [MBProgressHUD showError:@"最多输入700个字"];
        [self makeToast:@"最多输入700个字"];
        textView.text = [textView.text substringToIndex:self.maxLength];
        number = self.maxLength;
        
    }
    self.numLable.text = [NSString stringWithFormat:@"%ld/700",(long)number];
}

//-(void)textFiledEditChanged:(NSNotification *)obj{
//    UITextView *textView = (UITextView *)obj.object;
//    
//    NSString *toBeString = textView.text;
//    NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式
//    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
//        UITextRange *selectedRange = [textView markedTextRange];
//        //获取高亮部分
//        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
//        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
//        if (!position) {
//            if (toBeString.length > self.maxLength) {
//                textView.text = [toBeString substringToIndex:self.maxLength];
//            }
//        }
//        // 有高亮选择的字符串，则暂不对文字进行统计和限制
//        else{
//            
//        }
//    }
//    // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
//    else{
//        if (toBeString.length > self.maxLength) {
//            textView.text = [toBeString substringToIndex:self.maxLength];
//        }
//    }
//    
//    
//}

- (void)addPhotoClick {
    
    if ([self.addDelegate respondsToSelector:@selector(addImageClick)]) {
        
        [self.addDelegate addImageClick];
    }
}
//
//- (void)dealloc {
//    
//    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"UITextViewTextDidChangeNotification" object:nil];
//}

@end
